package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.market;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.data.dto.SelectResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.net_marketing.api.ConfigAPIService;
import com.gzhryc.net_marketing.api.protocol.config.LogisticsCompanyListResponse;
import com.gzhryc.net_marketing.supplier.Supplier;
import com.gzhryc.net_marketing.supplier.SupplierManager;
import com.gzhryc.net_marketing.supplier.config.dao.db.SupplierLogisticsCompany;
import com.gzhryc.net_marketing.supplier.config.dao.enums.SupplierLogisticsCompanyEnum;
import com.gzhryc.net_marketing.supplier.config.services.SupplierLogisticsCompanyService;
import com.gzhryc.net_marketing.supplier.config.services.dto.SupplierLogisticsCompanySearch;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.h5.agent.mng.WebConstants;

public class LogisticsCompanyController {

    @Route(value = { "/market/logistics_company/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("stateList", SupplierLogisticsCompanyEnum.State.values());
        return "/market/logistics_company/list.html";
    }

    @Route(value = "/market/logistics_company/list.data")
    public ListResult<SupplierLogisticsCompany> listData(HttpServletRequest request) {
        ListResult<SupplierLogisticsCompany> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        Supplier supplier = SupplierManager.getSupplier();
		if(supplier != null) {
	        SupplierLogisticsCompanySearch search = WebUtils.getParameter(request,SupplierLogisticsCompanySearch.class);
	        Integer pageNo = WebUtils.getInteger(request, "page", 1);
	        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
	
	        result.getBody().setTotal(SupplierLogisticsCompanyService.self(supplier.getJdbcKey()).countBySearch(search));
	        List<SupplierLogisticsCompany> logisticsCompanyList = SupplierLogisticsCompanyService.self(supplier.getJdbcKey()).findBySearch(search,pageNo,pageLimit);
	        if(logisticsCompanyList != null && logisticsCompanyList.size() > 0){
	            result.setCode(DtoConstants.SUCCESS,null);
	            result.getBody().setData(logisticsCompanyList);
	        }
		}
        return result;
    }

    @Route(value = { "/market/logistics_company/add.html" })
    public String add(HttpServletRequest request) {
        request.setAttribute("stateList", SupplierLogisticsCompanyEnum.State.values());
        return "/market/logistics_company/edit.html";
    }

    @Route(value = {"/market/logistics_company/add.do"})
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增失败，请稍后再试");
        Supplier supplier = SupplierManager.getSupplier();
		if(supplier != null) {
	        SupplierLogisticsCompany logisticsCompany = WebUtils.getParameter(request,SupplierLogisticsCompany.class);
	        String companyId = WebUtils.getString(request, "companyId");
	        if(StringTools.isNotBlank(companyId)) {
	        	String[] companyInfo = companyId.split("_");
	        	if(companyInfo != null && companyInfo.length == 2) {
	        		logisticsCompany.setId(companyInfo[0]);
	        		logisticsCompany.setName(companyInfo[1]);
	        		
	        		try {
	        			if(SupplierLogisticsCompanyService.self(supplier.getJdbcKey()).add(logisticsCompany,0)){
	        			    result.setCode(DtoConstants.SUCCESS,null);
	        			}
	        		} catch (LogicException e) {
	        			result.setMsg(e.getMessage());
	        		}
	        	}
	        }
		}
        return result;
    }

    @Route(value = { "/market/logistics_company/update.html" })
    public String update(HttpServletRequest request) {
        String id = WebUtils.getString(request,"id");
        Supplier supplier = SupplierManager.getSupplier();
		if(supplier != null && id != null) {
	        SupplierLogisticsCompany logisticsCompany = SupplierLogisticsCompanyService.self(supplier.getJdbcKey()).getById(id);
	        if(logisticsCompany != null) {
	        	WebUtils.setAttribute(request,logisticsCompany);
	        }
		}
		request.setAttribute("stateList", SupplierLogisticsCompanyEnum.State.values());
        return "/market/logistics_company/edit.html";
    }

    @Route(value = {"/market/logistics_company/update.do"})
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改失败，请稍后再试");
        Supplier supplier = SupplierManager.getSupplier();
		if(supplier != null) {
	        SupplierLogisticsCompany logisticsCompany = WebUtils.getParameter(request,SupplierLogisticsCompany.class);
	        try {
				if(SupplierLogisticsCompanyService.self(supplier.getJdbcKey()).updateNotNull(logisticsCompany,0)){
				    result.setCode(DtoConstants.SUCCESS,null);
				}
			} catch (LogicException e) {
				result.setMsg(e.getMessage());
			}
		}
        return result;
    }

    @Route(value = {"/market/logistics_company/del.do"})
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除失败，请稍后再试");
        String id = WebUtils.getString(request,"id");
        Supplier supplier = SupplierManager.getSupplier();
		if(supplier != null) {
	        try {
				if(SupplierLogisticsCompanyService.self(supplier.getJdbcKey()).deleteById(id)){
				    result.setCode(DtoConstants.SUCCESS,null);
				}
			} catch (LogicException e) {
				result.setMsg(e.getMessage());
			}
		}
        return result;
    }
    
    @Route("/market/logistics_company/cloud/select.data")
    public SelectResult selectData(HttpServletRequest request) {
        SelectResult result = new SelectResult(DtoConstants.SUCCESS);
        String term = request.getParameter("term");
        Integer page = WebUtils.getInteger(request, "page", 1);
        Long total = 0L;

        LogisticsCompanyListResponse apiResp = ConfigAPIService.self().findLogisticsCompanyList(term, page, WebConstants.PAGE_LIMIT);
        if(apiResp != null && DtoConstants.SUCCESS.equals(apiResp.getCode())) {
        	total = apiResp.getBody().getTotal();
        	
        	for (LogisticsCompanyListResponse.CompanyItem company : apiResp.getBody().getItems()) {
                SelectResult.Item item = new SelectResult.Item();
                item.setId(company.getId()+ "_" + company.getName());
                item.setText(company.getName());
                result.getBody().getData().add(item);
            }
        }
        result.getBody().setPage(page);
        result.getBody().setLimit(WebConstants.PAGE_LIMIT);
        result.getBody().setTotal(total);
        return result;
    } 
}
